﻿using EAMF.Content.Projectiles;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;

namespace EAMF.Content.Items.Weapon
{
    public class SniperRifles : ModItem
    {
        public override void SetStaticDefaults()
        {
            base.SetStaticDefaults();
        }
        public override void SetDefaults()
        {
            Item.width = 49;
            Item.height = 49;
            Item.useTime = 30;
            Item.useAnimation = 30;
            Item.useStyle = ItemUseStyleID.Shoot;
            Item.autoReuse = true;
            Item.useTurn = false;
            Item.UseSound = SoundID.Item1;
            Item.damage = 150;
            Item.DamageType = DamageClass.Ranged;
            Item.knockBack = 6f;
            Item.crit = 20;
            Item.noMelee = true;
            Item.shoot = ModContent.ProjectileType<Projectile_Star>();
            //Item.shoot = ProjectileID.Bullet;
            Item.shootSpeed = 100f;
            //Item.useAmmo = AmmoID.Arrow;//是否消耗弹药,这里会将Item.shoot屏蔽掉,然后使用背包的弹药
            Item.useAmmo = AmmoID.Bullet;//消耗子弹
            //base.SetDefaults();
        }
        public override void MeleeEffects(Player player, Rectangle hitbox)
        {
            if (Main.GameUpdateCount % 3 == 0)
            {
                Dust d = Dust.NewDustDirect(hitbox.TopLeft(), hitbox.Width, hitbox.Height, DustID.Ichor);
                base.MeleeEffects(player, hitbox);
                d.velocity *= 5;
                d.noGravity = true;
            }
        }
        public override void ModifyShootStats(Player player, ref Vector2 position, ref Vector2 velocity, ref int type, ref int damage, ref float knockback)
        {
            //在没有射击之前做出的处理，比如添加buff，添加状态等
            base.ModifyShootStats(player, ref position, ref velocity, ref type, ref damage, ref knockback);
        }
        //武器射击
        public override bool Shoot(Player player, EntitySource_ItemUse_WithAmmo source, Vector2 position, Vector2 velocity, int type, int damage, float knockback)
        {
            int mode = 2;
            switch (mode)
            {
                default:
                    break;
                case 0:
                    {
                        //发射一串散弹
                        //tr使用的时弧度制,MathHelper.Pi为半圆的角度，MathHelper.TwoPi / 5为每5颗子弹的间隔角度
                        //条件分别代表左右最大偏角，还有每5颗子弹的间隔角度
                        for (float r = -MathHelper.Pi * 0.05f; r <= MathHelper.Pi * 0.05f; r += MathHelper.TwoPi * 0.05f / 4f)
                        {
                            //-18,-10.8,-3.6,
                            float r2 = r + velocity.ToRotation();
                            //根据速度方向偏转子弹方向                                                     
                            //将角度转化为方向                              
                            //Vector2 v = new Vector2((float)Math.Cos(r2), (float)Math.Sin(r2 )) * 10f;
                            Vector2 v = r2.ToRotationVector2() * 20f;
                            v *= (1f - Math.Abs(r) * 2);//根据偏转角减速
                                                        //type = Item.useAmmo;//在这里写的不会被屏蔽，会直接发射出去
                                                        //type = Main.rand.Next(1, 10); //随机子弹
                                                        //Item.scale = Main.rand.NextFloat(10f, 20f); //随机缩放（整活）
                            Projectile.NewProjectile(source, position, v, type, damage, knockback, player.whoAmI);
                        }
                    }
                    break;
                case 1:
                    {
                        //ToRotationVector2
                        //发射散弹
                        //计算玩家到鼠标向量的弧度
                        Vector2 plrToMouse = Main.MouseWorld - player.Center;
                        float r = (float)Math.Atan2(plrToMouse.Y, plrToMouse.X);
                        for (int i = -2; i <= 2; i++)
                        {
                            //5°= MathHelper.Pi / 36
                            float r2 = r + i * MathHelper.Pi / 36;
                            velocity = r2.ToRotationVector2() * Item.shootSpeed;
                            Projectile.NewProjectile(source, position, velocity, type, damage, knockback, player.whoAmI);
                        }
                    }
                    break;
                case 2:
                    {
                        for (int i = -2; i <= 2; i++)
                        {
                            //围绕在玩家附近随机生成子弹
                            Vector2 projOr = new Vector2(Main.rand.Next(-100, 100), Main.rand.Next(-100, -10));
                            Vector2 projPos = position + projOr;
                            //子弹指向鼠标方向
                            Vector2 projToMouse = Main.MouseWorld - player.Center - projOr;
                            float r = (float)Math.Atan2(projToMouse.Y, projToMouse.X);
                            velocity = r.ToRotationVector2() * Item.shootSpeed;
                            Projectile.NewProjectile(source, projPos, velocity, type, damage, knockback, player.whoAmI);
                        }
                    }
                    break;
                case 3:
                    {

                    }
                    break;

            }
            return false;
        }
        /*
         *只作用于物品本身不作用于武器附带的弹药
         */
        public override void OnHitNPC(Player player, Terraria.NPC target, Terraria.NPC.HitInfo hit, int damageDone)
        {
            target.AddBuff(BuffID.OnFire, 300); //给目标添加火焰状态
            target.lifeMax -= 10000; //给目标减少生命值
            if (damageDone > 0)
            {
                player.HealEffect(damageDone / 2); //给玩家回复一半的生命值
                player.statLife += damageDone / 2; //给玩家增加生命值,不会显示在UI上
                player.Heal(damageDone / 2); //给玩家回复伤害值的一半
            }
            base.OnHitNPC(player, target, hit, damageDone);
        }
        public override Vector2? HoldoutOffset()
        {
            //内置的武器偏移
            return new Vector2(-10, 0); //更像是武器的中心点的偏移
                                        //return base.HoldoutOffset();
        }

        //修改玩家选中这个武器时的动作
        public override void HoldItemFrame(Player player)
        {
            Item.rare = Main.rand.Next(1, 10); //设置品质
                                               // 选中武器的时候设置为第3帧
                                               //player.bodyFrame.Y = player.bodyFrame.Height * 2;
        }

        //public override Vector2? HoldoutOrigin()
        //{
        //     //只对法术武器有效，返回武器的握持点坐标
        //    return base.HoldoutOrigin();
        //}

        //public override void HoldStyle(Player player)
        //{
        //    base.HoldStyle(player);
        //}
        //public override bool CanUseItem(Player player)
        //{
        //    // return true; 可以使用
        //    // return false; 不能使用
        //    // 假如是夜晚就不能用
        //    if (!Main.dayTime)
        //    {
        //        return false;
        //    }
        //    // 玩家在海洋就不能用
        //    if (player.ZoneBeach)
        //    {
        //        return false;
        //    }
        //    return true;
        //}
    }
}
